package com.angcyo.bmob.api

import cn.bmob.data.bean.op.BmobQuery
import cn.bmob.data.bean.table.BmobObject
import cn.bmob.data.callback.`object`.*
import cn.bmob.data.exception.BmobException

/**
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2021/01/19
 */

class DslBmobQuery<T> : BmobQuery() {

    var getAction: ((data: T?, ex: BmobException?) -> Unit)? = null

    var getsAction: ((dataList: List<T>?, ex: BmobException?) -> Unit)? = null

    var countAction: ((count: Int?, ex: BmobException?) -> Unit)? = null

    var deleteAction: ((msg: String?, ex: BmobException?) -> Unit)? = null

    var updateAction: ((updatedAt: String?, ex: BmobException?) -> Unit)? = null

    var saveAction: ((data: T?, ex: BmobException?) -> Unit)? = null
}

//<editor-fold desc="查询相关">

/**查询表*/
inline fun <reified T : BmobObject> bmobQuery(objectId: String, config: DslBmobQuery<T>.() -> Unit) {
    val query = DslBmobQuery<T>()
    query.config()
    query.getObject(objectId, object : GetListener<T>() {
        override fun onFailure(ex: BmobException?) {
            query.getAction?.invoke(null, ex)
        }

        override fun onSuccess(obj: T?) {
            query.getAction?.invoke(obj, null)
        }
    })
}

inline fun <reified T : BmobObject> bmobGet(objectId: String, config: DslBmobQuery<T>.() -> Unit) {
    bmobQuery(objectId, config)
}

inline fun <reified T : BmobObject> bmobQueryList(config: DslBmobQuery<T>.() -> Unit) {
    val query = DslBmobQuery<T>()
    query.config()
    query.getObjects(object : GetsListener<T>() {
        override fun onFailure(ex: BmobException?) {
            query.getsAction?.invoke(null, ex)
        }

        override fun onSuccess(array: MutableList<T>?) {
            query.getsAction?.invoke(array, null)
        }
    })
}

inline fun <reified T : BmobObject> bmobGets(config: DslBmobQuery<T>.() -> Unit) {
    bmobQueryList(config)
}

/**查询数量*/
inline fun <reified T : BmobObject> bmobCount(config: DslBmobQuery<T>.() -> Unit) {
    val query = DslBmobQuery<T>()
    query.config()
    query.getCount(object : CountListener<T>() {
        override fun onFailure(ex: BmobException?) {
            query.countAction?.invoke(null, ex)
        }

        override fun onSuccess(count: Int?) {
            query.countAction?.invoke(count, null)
        }
    })
}

//</editor-fold desc="查询相关">

//<editor-fold desc="删除相关">

fun <T : BmobObject> bmobDelete(bmobObj: T, config: DslBmobQuery<T>.() -> Unit) {
    val query = DslBmobQuery<T>()
    query.config()
    bmobObj.delete(object : DeleteListener() {
        override fun onFailure(ex: BmobException?) {
            query.deleteAction?.invoke(null, ex)
        }

        override fun onSuccess(msg: String?) {
            query.deleteAction?.invoke(msg, null)
        }
    })
}

//</editor-fold desc="删除相关">

//<editor-fold desc="更新相关">

/**更新一行数据*/
fun <T : BmobObject> bmobUpdate(bmobObj: T, config: DslBmobQuery<T>.() -> Unit) {
    val query = DslBmobQuery<T>()
    query.config()
    val oldObjectId = bmobObj.objectId
    val oldCreateAt = bmobObj.createdAt
    val oldAcl = bmobObj.acl
    bmobObj.update(object : UpdateListener() {
        override fun onFailure(ex: BmobException?) {
            query.updateAction?.invoke(null, ex)
        }

        //返回更新对象的时间
        override fun onSuccess(updatedAt: String?) {
            bmobObj.objectId = oldObjectId
            bmobObj.createdAt = oldCreateAt
            bmobObj.acl = oldAcl
            bmobObj.updatedAt = updatedAt
            query.updateAction?.invoke(updatedAt, null)
        }
    })
}

//</editor-fold desc="更新相关">

//<editor-fold desc="保存相关">

/**保存一行数据*/
fun <T : BmobObject> bmobSave(bmobObj: T, config: DslBmobQuery<T>.() -> Unit) {
    val query = DslBmobQuery<T>()
    query.config()
    bmobObj.save(object : SaveListener() {
        override fun onFailure(ex: BmobException?) {
            query.saveAction?.invoke(null, ex)
        }

        override fun onSuccess(objectId: String?, createdAt: String?) {
            bmobObj.objectId = objectId
            bmobObj.createdAt = createdAt
            query.saveAction?.invoke(bmobObj, null)
        }
    })
}

//</editor-fold desc="保存相关">
